 /*****************************************************************************
  *   Copyright 2005 Tim A Wang
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
  *   you may not use this file except in compliance with the License.
  *   You may obtain a copy of the License at
  *
  *       http://www.apache.org/licenses/LICENSE-2.0
  *
  *   Unless required by applicable law or agreed to in writing, software
  *   distributed under the License is distributed on an "AS IS" BASIS,
  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *   See the License for the specific language governing permissions and
  *   limitations under the License.
  *
  ****************************************************************************/
 package com.jb.web.renderer;

 import com.jb.web.bean.Panel;
 import com.jb.web.bean.Label;
 import com.jb.web.bean.TabbedPanel;
 import com.jb.web.context.WebContext;

 import java.util.Iterator;

 import com.jb.faces.component.UIComponent;


 public class TabStripRenderer extends RegionRenderer {
     public StringBuffer renderChildren(WebContext pCtx, UIComponent itemToRender) {
         StringBuffer html = new StringBuffer();
         TabbedPanel sect = (TabbedPanel) itemToRender;
         if(!sect.isHideTabs() || !pCtx.isRunTime())
             html.append(renderTabs(pCtx, sect));
         
         Panel subSect = sect.getSelected();

         if (subSect != null) {
             html.append(subSect.render(pCtx));
         }

         return html;
     }

     
     public StringBuffer tab(WebContext pCtx, Panel tabPanel) {
         StringBuffer html = new StringBuffer();
         TabbedPanel sect=(TabbedPanel) tabPanel.getParent();
         Label header = tabPanel.getHeader();
         html.append("<div class=\"jb_tab\" ");

         html.append(getEditScript(pCtx, header))
            .append(addTagProperty("style", header.getStyle(pCtx), null));

         html.append(" ><div class=\"jb_tab_tl\"><div class=\"jb_tab_tr\"><div class=\"jb_tab_tc\">");         
         html.append("<div onclick=\"JB._e('").append(pCtx.getPageName() + ".xsp','")
             .append(tabPanel.getClientId()).append("','com.jb.web.bean.event.OnSelectTab")
             .append("','").append(sect.getClientId()).append("');\"");

        html.append(addTagProperty("class", "jb_tab_inner " + header.getStyleClass(), null));
         
         html.append(">");
         if (!isEmptyString(header.getIconImage())) {
             html.append("<img  class=\"jb_tab_icon\" ");
             html.append(addTagProperty("src", header.getIconImage(), 
                                        null));
             html.append(" />");
         }

         String headLabel = evaluate(pCtx, header, header.getValue());
         if (isEmptyString(headLabel)) {
             headLabel = "&nbsp;&nbsp;";
         }
         html.append(headLabel);
         return html.append("</div></div></div> </div></div>");
     }
     
     //End of your change
     
     protected String renderTabs(WebContext pCtx, TabbedPanel sect) {
         Label header = sect.getHeader();
         StringBuffer html = new StringBuffer("<div class=\"jb_tab_top\" ")
             .append(addTagProperty("width", "100%", null))
         .append(addTagProperty("id", sect.getClientId()+"top", null))
         .append("><div class=\"jb_tab_wrap\" ><ul class=\"jb_tab_strip\"><li class=\"jb_tab_space\" style=\"background-color:transparent;\" >&nbsp;</li>");

         String headLabel = evaluate(pCtx, header, header.getValue());
         if (isEmptyString(headLabel)) {
             headLabel = "&nbsp;&nbsp";
         }

         Iterator iter = sect.getChildPanels().iterator();

         while (iter.hasNext()) {
             Panel aSect = (Panel) iter.next();
             if (aSect != null) {
                 Label aTitle = aSect.getHeader();

                 if (!aTitle.isVisible(pCtx)) {
                     continue;
                 }
                 
                 //aSect.getWidth();
                 //aSect.getHeight();
                 
                 if(sect.isSelected(aSect))
                    html.append("<li class=\"jb_tab_active_item\" ") 
                                            .append(addTagProperty("style", aTitle.getDimensionStyle(),null))
                                            .append("> ");
                 else
                    html.append("<li class=\"jb_tab_item\" ")
                        .append(addTagProperty("style", aTitle.getDimensionStyle(),null))
                        .append("onmouseover=\"Element.addClassName(this,'jb_tab_over');\" onmouseout=\"Element.removeClassName(this,'jb_tab_over');\" >");
                 html.append(tab(pCtx, aSect));
                 html.append("</li>");
             }
         }
         html.append("<span style=\"width:12px;background-color:transparent;\" >&nbsp;</span></ul>");
         html.append("</div><div class=\"x-tab-strip-spacer\" ></div></div>");
         

         return html.toString();
     }
 }
